package com.yoomiito.app.widget.dialog;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.ui.my.psw.SetPayPswActivity;
import com.yoomiito.app.widget.PasswordView_1;

public class PswInputDialog extends BaseDialog {

    private PasswordView_1 psw;
    private TextView mPswTv;
    private PasswordView_1.TextChangedListener mTextChangedListener;

    public PswInputDialog(@NonNull Context context) {
        this(context, 0);
    }

    public PswInputDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.dialog_2);
    }

    protected PswInputDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    public View setView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_psw_input, null, false);
        setmGravity(Gravity.BOTTOM);
        setWidthPercent(1);
        psw = view.findViewById(R.id.psw);
        mPswTv = view.findViewById(R.id.psd_error);
        view.findViewById(R.id.forget_psw).setOnClickListener(v -> {
                    if (mTextChangedListener != null) mTextChangedListener.forgetPsw();
                    SetPayPswActivity.start(mContext);
                }
        );
        psw.setTextChangedListener(mTextChangedListener);
        return view;
    }

    @Override
    public void show() {
        super.show();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        psw.requestFocus();
        psw.setFocusableInTouchMode(true);
    }

    public void setTextChangedListener(PasswordView_1.TextChangedListener textChangedListener) {
        mTextChangedListener = textChangedListener;
    }

    public void showErrorTip(boolean show, String tip) {
        mPswTv.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
        if (show) {
            mPswTv.setText(tip);
        }
        psw.clearText();
        psw.requestFocus();
        psw.setFocusableInTouchMode(true);

    }


}
